package com.example.ItPhotoAlbumCloud.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service

public class VerificationCodeGenerator {

    private final RedisTemplate<String, String> redisTemplate;

    public VerificationCodeGenerator(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public String generateCode(String email) {
        String code = String.format("%06d", new Random().nextInt(1000000));
        redisTemplate.opsForValue().set(code, email, 60, TimeUnit.SECONDS);
        //返回生成的验证码
        return code;
    }

}
